<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Soccer extends MY_Controller {

	private $insertedOdds = 0;
	private $sportId = 1;

	function __construct() {
		parent::__construct();
	}
	
	function index() {
		//$this->odds_model->clearOdds();
		foreach($this->bookie_model->getBookie() as $bookie) {
			$getHtmlByBookie = $this->html_model->getHtmlByBookie($bookie["bookie_name"], $this->sportId);
			if(!count($getHtmlByBookie)) continue;
			foreach($getHtmlByBookie as $htmlData) {
				$html = str_get_html($htmlData["html_text"]);
				$this->insertedOdds = 0;
				switch($bookie["bookie_name"]) {
				
					case "expekt": {
					
						foreach($html->find("div[class=cal-day-entry]") as $calDayEntry) {
							$odds_datetime = substr($calDayEntry->getAttribute("data-date"), 0, 10);
							
							foreach($calDayEntry->find("div[class=cal-schedule]") as $calSchedule) {
								if(count($calSchedule->find("p[class=event-name]"))) {
									$odds_league = $calSchedule->find("p[class=event-name]", 0)->plaintext;
								}
								$events = $calSchedule->find("div[class=event]");
								
								foreach($events as $ii => $event) {
								
									$insUpd = array();
									$insUpd["odds_league"] = $odds_league;
									$insUpd["odds_date"] = $insUpd["odds_datetime"] = $odds_datetime;
									if($event->find("div[class=cal-hour]", 0)) {
										$insUpd["odds_datetime"] .= " ".Common::prepare_str($event->find("div[class=cal-hour]", 0)->plaintext);
									}
									$match_name = preg_split("/ - /", $event->find("div[class=match-name]", 0)->plaintext);
									$insUpd["odds_team1"] = Common::prepare_str($match_name[0]);
									$insUpd["odds_team2"] = Common::prepare_str($match_name[1]);
									$insUpd["odds_type"] = $htmlData["html_type"];
									$insUpd["odds_html_id"] = $htmlData["html_id"];

									switch($htmlData["html_type"]) {
									
										case "3WAY":
										case "DOUBLECHANCE": {
											// 1X, 12, X2
											$insUpd["odds_odds1"] = Common::prepare_str($event->find("span[class=oddValue]", 0)->plaintext);
											$insUpd["odds_odds2"] = Common::prepare_str($event->find("span[class=oddValue]", 1)->plaintext);
											$insUpd["odds_odds3"] = Common::prepare_str($event->find("span[class=oddValue]", 2)->plaintext);
											//Common::pr($insUpd); exit;
											$this->odds_model->insert($insUpd);
											$this->insertedOdds++;
										} break;
										
										case "OVERUNDER": {
											for($i = 1; $i < count($event->find("span[class=oddValue]"))/2; $i++) {
												$insUpd["odds_type"] = $htmlData["html_type"]." ".str_replace(array("Under ", "Over "), array("", ""), Common::prepare_str($event->find("span[class=oddLabel]", $i*2)->plaintext));
												$insUpd["odds_odds1"] = Common::prepare_str($event->find("span[class=oddValue]", $i*2)->plaintext);
												$insUpd["odds_odds2"] = NULL;
												$insUpd["odds_odds3"] = Common::prepare_str($event->find("span[class=oddValue]", $i*2+1)->plaintext);
												//Common::pr($insUpd);
												$this->odds_model->insert($insUpd);
												$this->insertedOdds++;
											}
										} break;
										
										case "DRAWNOBET": {
											$insUpd["odds_odds1"] = Common::prepare_str($event->find("span[class=oddValue]", 0)->plaintext);
											$insUpd["odds_odds2"] = NULL;
											$insUpd["odds_odds3"] = Common::prepare_str($event->find("span[class=oddValue]", 1)->plaintext);
											//Common::pr($insUpd); exit;
											$this->odds_model->insert($insUpd);
											$this->insertedOdds++;
										} break;
										
									}
								}
							
							}
							
						}
					} break;
					
					case "bet-at-home": {
						
						$tables = $html->find("table[class=ods s-expanded]");
						foreach($tables as $ii => $table) {
							$league = $table->parent()->prev_sibling()->find("h1[class=hea-text]")[0]->plaintext;
							
							foreach($table->find("tr") as $jj => $tr) {
								if($jj) {
									$teams = preg_split("/ - /", $tr->find("td", 0)->plaintext);
									$insUpd = array();
									$insUpd["odds_html_id"] = $htmlData["html_id"];
									$insUpd["odds_type"] = $htmlData["html_type"];
									$insUpd["odds_league"] = $league;
									$date = Common::prepare_str($tr->find("td[class=date]", 0)->plaintext);
									$odds_date = "20".substr($date, 6, 2).".".substr($date, 3, 2).".".substr($date, 0, 2);
									$insUpd["odds_date"] = $odds_date;
									$insUpd["odds_datetime"] = $odds_date." ".substr($date, 9);
									$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
									$insUpd["odds_team2"] = Common::prepare_str($teams[1]);
									
									switch($htmlData["html_type"]) {
									
										case "3WAY":
										case "DOUBLECHANCE": {
											$insUpd["odds_odds1"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 0)->plaintext);
											$insUpd["odds_odds2"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 1)->plaintext);
											$insUpd["odds_odds3"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 2)->plaintext);
										} break;
										
										case "UNDEROVER":
											$team2 = preg_match("/(.*)\(under\/over ([^\)]*)\)/", $insUpd["odds_team2"], $matches);
											$insUpd["odds_team2"] = $matches[1];
											$insUpd["odds_type"] .= " ".$matches[2];
										case "DRAWNOBET":
										case "DOUBLECHANCE": {
											$insUpd["odds_odds1"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 0)->plaintext);
											$insUpd["odds_odds2"] = NULL;
											$insUpd["odds_odds3"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 1)->plaintext);
										} break;
										
									}
									//Common::pr($insUpd);
									$this->odds_model->insert($insUpd);
									$this->insertedOdds++;
								}
							}
							
						}
					} break;
					
				}
				
				
				$html->clear(); 
				unset($html);
				// html update
				$this->html_model->update($htmlData["html_id"], array(
					"html_parsed_time" => Common::now(),
					"html_inserted_odds" => $this->insertedOdds,
				));
			}
		}
		
		// odds fix/mapping
		//$this->odds_model->tableFix();
		$this->odds_model->mapping();
	}

}